﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
    bool createAgain = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Count > 0) {
            for (int i = 1; i <= Session.Count; i++)
            {
                SurveyQuestion control = (SurveyQuestion)Session[(i).ToString()];
                pnlUserControls.Controls.Add(control);
            }

        }
    }
    protected void btnCreateControls_Click(object sender, EventArgs e)
    {
        int count = Convert.ToInt32(txtHowMany.Text);
        foreach (ListItem li in chkblControlTypes.Items)
        {
            if (li.Selected)
            {
                if (li.Value == "Label")
                {
                    for (int i = 1; i <= count; i++)
                    {
                        Label lbl = new Label();
                        lbl.Text = "Label - " + i.ToString();
                        pnlLabel.Controls.Add(lbl);
                    }
                }
                else if (li.Value == "TextBox")
                {
                    for (int i = 1; i <= count; i++)
                    {
                        TextBox txtBox = new TextBox();
                        txtBox.Text = "Text Box - " + i.ToString();
                        pnlTextBox.Controls.Add(txtBox);
                    }
                }
                else if (li.Value == "Button")
                {
                    for (int i = 1; i <= count; i++)
                    {
                        Button btnControl = new Button();
                        btnControl.Text = "Button - " + i.ToString();
                        pnlButton.Controls.Add(btnControl);
                    }
                }
            }
        }
    }


    protected void btnCreateUserControl_Click(object sender, EventArgs e)
    {
        SurveyQuestion myQuestion = LoadControl("~/MyControls/SurveyQuestion.ascx") as SurveyQuestion;
        pnlUserControls.Controls.Add(myQuestion);        
        Session.Add((Session.Count + 1).ToString(), myQuestion);
        createAgain =true;
    }

    //protected Control GetPostBackControl(Page page)
    //{
    //    Control control = null;
    //    try
    //    {
    //        string ctrlName = page.Request.Params.Get("__EVENTTARGET");

    //        if (ctrlName != null && ctrlName != String.Empty)
    //        {
    //            control = page.FindControl(ctrlName);
    //        }
    //        else
    //        {
    //            Panel cph = (Panel)page.FindControl("surveyquestion");
    //            for (int i = 0, len = page.Request.Form.Count; i < len; i++)
    //            {
    //                string[] ctl = page.Request.Form.AllKeys[i].Split('$');
    //                if (ctl.Length > 2)
    //                {
    //                    control = cph.FindControl(ctl[2]) as System.Web.UI.WebControls.Button;
    //                }

    //                if (control != null) break;
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        throw ex;
    //    }
    //    return control;
    //}

    //protected override void OnPreInit(EventArgs e)
    //{
    //    base.OnPreInit(e);

    //    Control control = GetPostBackControl(this.Page);

    //    if ((control != null && control.ClientID == btnCreateControls.ClientID) || createAgain)
    //    {
    //        //should be set before the CreateUserControl method
    //        createAgain = true;

    //        CreateUserControl();
    //    }
    //}

    //protected void CreateUserControl()
    //{

    //    try
    //    {
    //        if (createAgain && pnlUserControls != null)
    //        {
    //            if (Session.Count > 0)
    //            {
    //                pnlUserControls.Controls.Clear();
    //                for (int i = 0; i < Session.Count; i++)
    //                {
    //                    switch (Session[i].ToString())
    //                    {
    //                        case "ASP.mycontrols_surveyquestion_ascx":
    //                            {
                                    
    //                                SurveyQuestion ucSimpleControl = LoadControl("~/MyControls/SurveyQuestion.ascx") as SurveyQuestion;

                                    
    //                                pnlUserControls.Controls.Add(ucSimpleControl);
    //                                break;
    //                            }
    //                    }
    //                }
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        throw ex;
    //    }
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Control control = GetPostBackControl(this.Page);
        //Response.Write(control.Controls.Count.ToString());
    }
}